import {fromJS} from "immutable";
import {
    change_page_total_count,
    save_author_article_list,
    save_pub_account_author,
    update_choice_author_index
} from "./contants";

const artMap = new Map();

const defaultStatus = fromJS({
    pubAuthorList: [],   //公众号作者
    pubChoiceIndex: 0,   //选中的公众号作者 位置
    pubArticleMap: fromJS(artMap),  //公众号文章列表  Map(id:Map(page:[...art]))
    showArticleList: [],   //要展示的列表
    pageTotalCount:0,// 总的页数
    pubSearchText: [],  //搜索框

});


export default (status = defaultStatus, action) => {
    switch (action.type) {
        case save_pub_account_author:
            return status.merge({
                pubAuthorList: fromJS(action.data)
            });
        case update_choice_author_index:
            return status.set("pubChoiceIndex", action.index);

        case save_author_article_list:
            const id = action.id;
            const page = action.pageIndex;
            const data = action.data;

            const pubArticleMap = status.get("pubArticleMap");
            if (pubArticleMap.has(id)) {  //是否包含 一级数据
                const newMap = pubArticleMap.get(id);

                if (newMap.has(page)) {  //是否有当前页码的数据
                    console.log(`含有当前页码 +> `);
                    const artList = newMap.get(page);
                    console.log(artList);

                    console.log(status.get("showArticleList"));
                    return status.merge({
                        showArticleList: artList,
                    });
                } else {
                    const getData = fromJS(data);
                    newMap.set(page, getData);
                    const update = pubArticleMap.set(id, newMap);

                    console.log(`不包含当前页码 +> ${update}`);
                    return status.merge({
                        showArticleList: getData,
                        pubArticleMap: update
                    });
                }
            } else {
                const newMap = fromJS(new Map());
                newMap.set(page, fromJS(data));
                pubArticleMap.set(id, newMap);

                const getData = fromJS(data);
                console.log(`不包含当前id +> ${pubArticleMap}`);
                console.log(pubArticleMap);

                return status.merge({
                    showArticleList: getData,
                    pubArticleMap: pubArticleMap
                });
            }

        case change_page_total_count:

            return status.set("pageTotalCount",action.pageCount);

        default:
            return status;
    }
}